package main.java;


import java.util.concurrent.atomic.AtomicInteger;

//原子性问题
public class test01 {

    public static void main(String[] args) {

        MyData test=new MyData();

        for (int i = 0; i < 20; i++) {
            new Thread(new Runnable() {

                @Override
                public void run() {
                    for (int j = 0; j < 1000; j++) {
                        test.set();
                    }
                }

            }).start();
        }

        while(Thread.activeCount()>2) {
            Thread.yield();
        }
        System.out.println(test.get());
    }
}

class MyData{
    AtomicInteger num=new AtomicInteger(0);

    public AtomicInteger get() {
        return num;
    }

    public void set() {
        num.getAndIncrement();
    }

}
